package com.zelin.user.controller;

import com.zelin.user.service.TbUserService;
import com.zyg.common.utils.PhoneFormatCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/8-15:41
 * ------------------------------
 */
@Controller
@RequestMapping
public class UserController {
    @Autowired
    private TbUserService userService;

    @GetMapping({"/register.html"})
    public String register(){

        return "register";
    }
    @GetMapping("/")
    public String index(){
        // String name = SecurityContextHolder.getContext().getAuthentication().getName();
        // System.out.println("name = " + name);
        // model.addAttribute("name",name);
        return "home-index";
    }

    //1. 获取手机验证码
    @GetMapping("/user/getCode")
    public String getCode(String phone){
        //1.1 如果手机号合法，就以其为key，以随机生成的验证码为值放到redis中
        if(PhoneFormatCheckUtils.isPhoneLegal(phone)){
            //1.2 开始发送验证码（① 保存code到redis ② 将验证码发送到消息的监听方）
            userService.sendCode(phone);
            //1.3
        }
        return "register";
    }
}
